<template>
  <el-dialog title="新增员工" :visible="showDialog" width="40%" @close="close">
    <!-- 表单 -->
    <!-- 表单 -->
    <el-form ref="form" label-width="120px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="form.username" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="form.mobile" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="form.timeOfEntry" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="form.formOfEmployment" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="form.workNumber" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <!-- <el-input v-model="form.departmentName" placeholder="请选择部门" /> -->
        <el-cascader
          v-model="form.departmentName"
          :options="list"
          :props="defaultProps"
          @focus="showDepts"
        />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="form.correctionTime" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-button size="small" @click="close">取消</el-button>
        <el-button type="primary" size="small" @click="addEmp">确定</el-button>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { ArrToTree } from '@/utils'
import Employees from '@/api/constant/employees'
import { getDepts } from '@/api/department'
import { addEmployee } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: ['blur', 'change'] },
          { pattern: /^[\u4e00-\u9fa5]{2,4}$/, message: '用户姓名为2-4位', trigger: ['blur', 'change'] }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: ['blur', 'change'] },
          { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: ['blur', 'change'] }
        ],
        formOfEmployment: [
          { required: true, message: '聘用形式不能为空', trigger: ['blur', 'change'] }
        ],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: ['blur', 'change'] }
        ],
        departmentName: [
          { required: true, message: '部门不能为空', trigger: ['blur', 'change'] }
        ],
        timeOfEntry: [
          { required: true, message: '请选择入职时间', trigger: ['blur', 'change'] }
        ]
      },
      options: Employees.hireType, // 聘用形式 使用的枚举里面数据
      list: [], // 级联
      defaultProps: {
        label: 'name', // 对应选择的项
        value: 'name', // 对应选择的值
        expandTrigger: 'hover' // 鼠标悬浮
      }
    }
  },
  methods: {
    close() {
      this.$emit('update:showDialog', false)
      this.form = {
        username: '',
        mobile: '',
        formOfEmployment: '',
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: ''
      }
    },
    // 2.获取部门列表
    async showDepts() {
      const res = await getDepts()
      console.log(res)
      // 3.完成级联展示
      this.list = ArrToTree(res.depts, '')
    },
    // 3.添加员工
    async addEmp() {
      try {
        await this.$refs.form.validate()
      } catch {
        return
      }
      // 这里注意 级联获取到的数据是数组
      await addEmployee({ ...this.form, departmentName: this.form.departmentName.slice(-1)[0] })
      this.$message.success('添加员工成功')
      this.$emit('addEmp', false)
    }
  }
}
</script>

<style>

</style>

